﻿using System;
using System.Collections.Generic;
using CookComputing.XmlRpc;
//
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI.Methods
{
   public class listWebhookAdd : Api_BaseExecute
   {
      private new listWebhookAddInput Input { get; set; }
      private listWebhookAddOutput Output { get; set; }
      // =============================================== constructors
      public listWebhookAdd() { }
      // ---------
      public listWebhookAdd( listWebhookAddInput input ) { Input = input; }
      // =============================================== Execute
      public listWebhookAddOutput Execute()
      {
         // empty constructor & Execute() is bad
         if ( Input == null )
            throw new ArgumentException( "Input class not provided" );
         // do the deed
         Output = Execute( Input );
         return Output;
      }
      // ---------------------------------------------
      public listWebhookAddOutput Execute( listWebhookAddInput input )
      {
         Input = input;
         Output = new listWebhookAddOutput( Input );
         ValidationIsOK = true;
         //------------------------- validate it
         if ( Input.api_Validate )
         {
            Output.api_ValidatorMessages = ValidateInput.Validate( Input );
            ValidationIsOK = ValidateInput.IsOK( Output.api_ValidatorMessages );
         }
         //------------------------- do it
         if ( ValidationIsOK )
         {
            ApiRun( Input, input.parms.apikey );  // execute API & format output
         }
         // ---
         return Output;
      }

      #region ================================================================= API Calls
      // ---------------------------------------------------------- XmlRpc
      protected override void ExecuteXmlRpc()
      {
         try
         {
            XmlRpcStruct actions = FormatActions( Input.parms.webhook_actions );
            XmlRpcStruct sources = FormatSources( Input.parms.webhook_sources );
            // ---
            bool rpcResult = apiXmlRpc.api.listWebhookAdd(
               Input.parms.apikey, Input.parms.id, Input.parms.url, actions, sources );
            Output.Format( rpcResult );
         }
         catch ( XmlRpcFaultException fex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( fex ) );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }

      }
      // --- format input
      private XmlRpcStruct FormatActions( Dictionary<EnumValues.listWebhookActions, bool> actions )
      {
         XmlRpcStruct rpcActions = new XmlRpcStruct();
         foreach ( KeyValuePair<EnumValues.listWebhookActions, bool> action in actions )
         {
            rpcActions.Add( action.Key.ToString(), action.Value );
         }
         return rpcActions;
      }
      private XmlRpcStruct FormatSources( Dictionary<EnumValues.listWebhookSources, bool> sources )
      {
         XmlRpcStruct rpcSources = new XmlRpcStruct();
         foreach ( KeyValuePair<EnumValues.listWebhookSources, bool> source in sources )
         {
            rpcSources.Add( source.Key.ToString(), source.Value );
         }
         return rpcSources;
      }

      // ---------------------------------------------------------- Serial Post
      protected override void ExecuteSerialPost()
      {
         try
         {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add( "apikey", Input.parms.apikey );
            dict.Add( "id", Input.parms.id );
            dict.Add( "url", Input.parms.url );

            foreach ( KeyValuePair<EnumValues.listWebhookActions, bool> action in Input.parms.webhook_actions )
            {
               dict.Add( string.Format( "actions[{0}]", action.Key.ToString() ), action.Value.ToString() );
            }
            foreach ( KeyValuePair<EnumValues.listWebhookSources, bool> source in Input.parms.webhook_sources )
            {
               dict.Add( string.Format( "sources[{0}]", source.Key.ToString() ), source.Value.ToString() );
            }
            // -- do it
            string api_Request = "";
            string api_Response = apiSerial.ProcessRequest( Input, dict, out api_Request );
            Output.Format( api_Request, api_Response );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      #endregion
   }
}
